package com.java110.mall.cart.bmo.storeOrderCart.impl;

import com.java110.dto.file.FileRelDto;
import com.java110.dto.product.ProductSpecDetailDto;
import com.java110.dto.shop.ShopDto;
import com.java110.dto.storeOrder.StoreOrderCartDto;
import com.java110.mall.cart.bmo.storeOrderCart.IGetStoreOrderCartBMO;
import com.java110.intf.system.IFileRelInnerServiceSMO;
import com.java110.intf.goods.IProductSpecDetailInnerServiceSMO;
import com.java110.intf.cart.IStoreOrderCartInnerServiceSMO;
import com.java110.intf.shop.IShopInnerServiceSMO;
import com.java110.utils.cache.MappingCache;
import com.java110.utils.constant.MappingConstant;
import com.java110.utils.util.ListUtil;
import com.java110.utils.util.StringUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("getStoreOrderCartBMOImpl")
public class GetStoreOrderCartBMOImpl implements IGetStoreOrderCartBMO {

    @Autowired
    private IStoreOrderCartInnerServiceSMO storeOrderCartInnerServiceSMOImpl;

    @Autowired
    private IProductSpecDetailInnerServiceSMO productSpecDetailInnerServiceSMOImpl;

    @Autowired
    private IFileRelInnerServiceSMO fileRelInnerServiceSMOImpl;

    @Autowired
    private IShopInnerServiceSMO shopInnerServiceSMOImpl;

    /**
     * @param storeOrderCartDto
     * @return 订单服务能够接受的报文
     */
    public ResponseEntity<String> get(StoreOrderCartDto storeOrderCartDto) {


        int count = storeOrderCartInnerServiceSMOImpl.queryStoreOrderCartsCount(storeOrderCartDto);

        List<StoreOrderCartDto> storeOrderCartDtos = null;
        if (count > 0) {
            storeOrderCartDtos = storeOrderCartInnerServiceSMOImpl.queryStoreOrderCarts(storeOrderCartDto);
            freshOrderCart(storeOrderCartDtos);
        } else {
            storeOrderCartDtos = new ArrayList<>();
        }

        ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) storeOrderCartDto.getRow()), count, storeOrderCartDtos);

        ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);

        return responseEntity;
    }

    /**
     * @param storeOrderCartDto
     * @return 订单服务能够接受的报文
     */
    public ResponseEntity<String> queryStoreOrderCartAdminInfo(StoreOrderCartDto storeOrderCartDto) {


        int count = storeOrderCartInnerServiceSMOImpl.queryStoreOrderCartsCount(storeOrderCartDto);

        List<StoreOrderCartDto> storeOrderCartDtos = null;
        if (count > 0) {
            storeOrderCartDtos = storeOrderCartInnerServiceSMOImpl.queryStoreOrderCartAdminInfo(storeOrderCartDto);
            String[] shopId = null;
            for (int storeOrderCartDtosIndex = 0; storeOrderCartDtosIndex < storeOrderCartDtos.size(); storeOrderCartDtosIndex++) {
                shopId = new String[storeOrderCartDtos.size()];
                shopId[storeOrderCartDtosIndex] = storeOrderCartDtos.get(storeOrderCartDtosIndex).getShopId();
            }
            //查询关联shop
            List<ShopDto> shopDtos = null;
            if (null != shopId && shopId.length > 0) {
                shopDtos = shopInnerServiceSMOImpl.getOrderShopInfo(shopId);
            }
            //匹配商品对应的中文名称
            if (null != shopDtos && shopDtos.size() > 0) {
                for (StoreOrderCartDto storeOrder : storeOrderCartDtos) {
                    for (ShopDto shopDto : shopDtos) {
                        if (storeOrder.getShopId().equals(shopDto.getShopId())) {
                            storeOrder.setShopName(shopDto.getShopName());
                        }
                    }
                }
            }
            //freshOrderCart(storeOrderCartDtos);
        } else {
            storeOrderCartDtos = new ArrayList<>();
        }

        ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) storeOrderCartDto.getRow()), count, storeOrderCartDtos);

        ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);

        return responseEntity;
    }

    /**
     * 刷新规格
     *
     * @param storeOrderCartDtos
     */
    private void freshOrderCart(List<StoreOrderCartDto> storeOrderCartDtos) {

        List<String> specIds = new ArrayList<>();

        for (StoreOrderCartDto storeOrderCartDto : storeOrderCartDtos) {
            specIds.add(storeOrderCartDto.getSpecId());
        }

        ProductSpecDetailDto productSpecDetailDto = new ProductSpecDetailDto();
        productSpecDetailDto.setSpecIds(specIds.toArray(new String[specIds.size()]));
        List<ProductSpecDetailDto> productSpecDetailDtos = productSpecDetailInnerServiceSMOImpl.queryProductSpecDetails(productSpecDetailDto);
        List<ProductSpecDetailDto> tmpProductSpecDetailDtos = null;
        if (!ListUtil.isNull(productSpecDetailDtos)) {
            for (StoreOrderCartDto storeOrderCartDto : storeOrderCartDtos) {
                if (StoreOrderCartDto.CART_TYPE_SERV.equals(storeOrderCartDto.getCartType())) {
                    continue;
                }
                tmpProductSpecDetailDtos = new ArrayList<>();
                for (ProductSpecDetailDto tmpProductSpecDetailDto : productSpecDetailDtos) {
                    if (!StringUtil.isEmpty(storeOrderCartDto.getSpecId()) && storeOrderCartDto.getSpecId().equals(tmpProductSpecDetailDto.getSpecId())) {
                        tmpProductSpecDetailDtos.add(tmpProductSpecDetailDto);
                    }
                }
                storeOrderCartDto.setProductSpecDetails(tmpProductSpecDetailDtos);
            }
        }

        //输入
        List<String> productIds = new ArrayList<>();

        for (StoreOrderCartDto tmpProductDto : storeOrderCartDtos) {
            productIds.add(tmpProductDto.getProductId());
        }

        FileRelDto fileRelDto = new FileRelDto();
        fileRelDto.setObjIds(productIds.toArray(new String[productIds.size()]));
        fileRelDto.setRelTypeCd(FileRelDto.REL_TYPE_CD_GOODS_COVER);
        List<FileRelDto> fileRelDtos = fileRelInnerServiceSMOImpl.queryFileRels(fileRelDto);

        //刷入图片信息
        List<String> photoVos = null;
        String url = null;
        String imgUrl = MappingCache.getValue(MappingConstant.FILE_DOMAIN, "IMG_PATH");
        imgUrl += (!StringUtil.isEmpty(imgUrl) && imgUrl.endsWith("/") ? "" : "/");
        for (StoreOrderCartDto tmpProductDto : storeOrderCartDtos) {
            for (FileRelDto tmpFileRelDto : fileRelDtos) {
                if (!tmpProductDto.getProductId().equals(tmpFileRelDto.getObjId())) {
                    //url = "/callComponent/download/getFile/file?fileId=" + tmpFileRelDto.getFileRealName() + "&communityId=-1";
                    continue;
                }

                if (StringUtil.isEmpty(tmpFileRelDto.getFileRealName())) {
                    continue;
                }
                url = imgUrl + tmpFileRelDto.getFileRealName();
                if (tmpFileRelDto.getFileRealName().startsWith("http")) {
                    url = tmpFileRelDto.getFileRealName();
                }

                tmpProductDto.setCoverPhoto(url);
            }
        }
    }

}
